package com.molichuxing.gateway.bff.car.services.impl;

import com.alibaba.fastjson.JSON;
import com.moli.iov.util.CommonUtils;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.car.entity.response.CarsMileageMonitoringEnterpriseVo;
import com.molichuxing.gateway.bff.car.entity.response.CarsMileageWarnEnterpriseVo;
import com.molichuxing.gateway.bff.car.services.CarsMileageMonitoringService;
import com.molichuxing.services.infrastructure.dto.request.create.CarsMileageWarnEnterpriseDto;
import com.molichuxing.services.infrastructure.dto.response.CarsMileageMonitoringEnterpriseResDto;
import com.molichuxing.services.infrastructure.dto.response.CarsMileageWarnEnterpriseResDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseDto;
import com.molichuxing.services.infrastructure.service.EnterpriseService;
import com.molichuxing.services.infrastructure.service.ICarsMileageMonitoringEnterpriseService;
import com.molichuxing.services.infrastructure.service.ICarsMileageWarnEnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 里程监控
 * The type Cars mileage monitoring enterprise service.
 *
 * @author wanghualin
 * @date 2020 -06-03 16:05:14
 */
@Service
@Slf4j
public class CarsMileageMonitoringServiceImpl implements CarsMileageMonitoringService {


    @Resource
    private ICarsMileageMonitoringEnterpriseService iCarsMileageWarnEnterpriseService;

    @Resource
    private ICarsMileageWarnEnterpriseService carsMileageWarnEnterpriseService;
    @Resource
    private EnterpriseService enterpriseService;


    @Override
    public Paged<CarsMileageMonitoringEnterpriseVo> listCarsMileageMonitoring(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        Paged<CarsMileageMonitoringEnterpriseResDto> dto = iCarsMileageWarnEnterpriseService.listCarsMileageMonitoring(map, pageNum, pageSize);
        Paged<CarsMileageMonitoringEnterpriseVo> voPaged = new Paged<>();
        voPaged.setPageNum(pageNum);
        voPaged.setPageSize(pageSize);
        voPaged.setTab(dto.getTab());
        voPaged.setTotal(dto.getTotal());
        voPaged.setList(ConvertDtoToVo(dto.getList()));
        if (CollectionUtils.isNotEmpty(voPaged.getList())) {
            List<Integer> collect = voPaged.getList().stream().map(CarsMileageMonitoringEnterpriseVo::getEnterpriseId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                return voPaged;
            }

            Map<Integer, EnterpriseDto> enterpriseMap = getEnterpriseByIds(collect);
            if (CollectionUtils.isEmpty(enterpriseMap)) {
                return voPaged;
            }
            for (CarsMileageMonitoringEnterpriseVo vo : voPaged.getList()) {
                vo.setEndTime(vo.getEndTime().minusDays(1));
                EnterpriseDto enterpriseDto = enterpriseMap.get(vo.getEnterpriseId());
                if (enterpriseDto!=null){
                    vo.setEnterpriseName(enterpriseDto.getName());
                }
            }

        }
        return voPaged;
    }

    /**
     * 得到公司信息
     *
     * @param collect
     * @return
     */
    private Map<Integer, EnterpriseDto> getEnterpriseByIds(List<Integer> collect) {
        return enterpriseService.getEnterpriseByIds(collect);
    }

    ;

    @Override
    public Paged<CarsMileageWarnEnterpriseVo> listCarsMileageWarnEnterprise(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        Paged<CarsMileageWarnEnterpriseResDto> dto = carsMileageWarnEnterpriseService.listCarsMileageWarnEnterprise(map, pageNum, pageSize);
        log.info("里程监控-列表-dto，" + JSON.toJSONString(dto));
        Paged<CarsMileageWarnEnterpriseVo> voPaged = new Paged<>();
        voPaged.setPageNum(pageNum);
        voPaged.setPageSize(pageSize);
        voPaged.setTab(dto.getTab());
        voPaged.setTotal(dto.getTotal());
        voPaged.setList(ConvertCarsMileageWarnEnterpriseDtoToVo(dto.getList()));
        if (CollectionUtils.isNotEmpty(voPaged.getList())) {
            List<Integer> collect = voPaged.getList().stream().map(CarsMileageWarnEnterpriseVo::getEnterpriseId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                return voPaged;
            }
            Map<Integer, EnterpriseDto> enterpriseMap = getEnterpriseByIds(collect);
            if (CollectionUtils.isEmpty(enterpriseMap)) {
                return voPaged;
            }
            for (CarsMileageWarnEnterpriseVo vo : voPaged.getList()) {
                EnterpriseDto enterpriseDto = enterpriseMap.get(vo.getEnterpriseId());
                vo.setEndTime(vo.getEndTime().minusDays(1));
                if (enterpriseDto!=null){
                    vo.setEnterpriseName(enterpriseDto.getName());
                }
            }

        }


        log.info("里程监控-列表-vo，" + JSON.toJSONString(dto));
        return voPaged;
    }

    @Override
    public ResponseResult updateCarsMileageWarnEnterprise(Map<String, Object> map) {

        String note = ObjectCastUtil.castString(map.get("note"));

        if (note.length() > 200) {
            return new ResponseResult().error("处理意见长度最多200个字");
        }
        CarsMileageWarnEnterpriseDto dto = new CarsMileageWarnEnterpriseDto();
        dto.setNote(note);
        dto.setId(ObjectCastUtil.castInteger(map.get("id")));
        dto.setGmtModified(LocalDateTime.now());
        dto.setStatus(ObjectCastUtil.castInteger(map.get("status")));
        Integer integer = carsMileageWarnEnterpriseService.updateCarsMileageWarnEnterprise(dto);
        if (integer == 1) {
            return new ResponseResult().success();
        } else {
            return new ResponseResult().error("更新失败");
        }
    }

    @Override
    public CarsMileageWarnEnterpriseVo getCarsMileageWarnEnterprise(Map<String, Object> map) {
        Integer integer = ObjectCastUtil.castInteger(map.get("id"));
        if (integer == null) {
            return null;
        }
        CarsMileageWarnEnterpriseResDto carsMileageWarnEnterprise = carsMileageWarnEnterpriseService.getCarsMileageWarnEnterprise(integer);
        CarsMileageWarnEnterpriseVo vo = new CarsMileageWarnEnterpriseVo();
        BeanUtils.copyProperties(carsMileageWarnEnterprise, vo);
        return vo;
    }

    /**
     * dto转vo
     *
     * @param source the source
     * @return the list
     */
    public List<CarsMileageMonitoringEnterpriseVo> ConvertDtoToVo(
            List<CarsMileageMonitoringEnterpriseResDto> source) {
        List<CarsMileageMonitoringEnterpriseVo> targetList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)) {
            for (CarsMileageMonitoringEnterpriseResDto o : source) {
                CarsMileageMonitoringEnterpriseVo vo = new CarsMileageMonitoringEnterpriseVo();
                CommonUtils.copyProperties(o, vo);
                targetList.add(vo);
            }
        }
        return targetList;
    }

    /**
     * dto转vo
     *
     * @param source the source
     * @return the list
     */
    public List<CarsMileageWarnEnterpriseVo> ConvertCarsMileageWarnEnterpriseDtoToVo(
            List<CarsMileageWarnEnterpriseResDto> source) {
        List<CarsMileageWarnEnterpriseVo> targetList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)) {
            for (CarsMileageWarnEnterpriseResDto o : source) {
                CarsMileageWarnEnterpriseVo vo = new CarsMileageWarnEnterpriseVo();
                CommonUtils.copyProperties(o, vo);
                targetList.add(vo);
            }
        }
        return targetList;
    }
}
